//
//  BookAdditions.m
//  Doc Ebook
//
//  Created by Hai Nguyen on 9/6/12.
//  Copyright (c) 2012 Hai Nguyen. All rights reserved.
//

#import "AppDelegate.h"
#import "BookAdditions.h"
#import "CoreDataHelper.h"

@implementation Book (Additions)

+ (BOOL)insertBook:(int)bookId title:(NSString*)title subtitle:(NSString*)subTitle excerpt:(NSString*)excerpt author:(NSString*)author pages:(int)pages favorite:(BOOL)favorite coverLink:(NSString*)coverLink{
	
	
	return YES;
}

+ (NSArray*)getAllBooks{
	NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;
	}
	NSArray *lists = nil;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Book" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		DEBUG_LOG(@"%@",[e description]);
    }
	
	return lists;
}

+ (NSArray*)getBooksInCategory:(int)catId{
	NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;
	}
    if (catId < 1) {
        return nil;
    }
    NSArray *resultArray = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"catId = %d", catId];
    @try{
        resultArray = (NSArray*) [CoreDataHelper searchObjectsInContext:@"Book" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e){
        DEBUG_LOG(@"search by phone number error: %@", [e description]);
    }
    
    return resultArray;
}

+ (Book*)bookWithId:(int)bookId{
	NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;
	}
    if (bookId < 1) {
        return nil;
    }
    NSArray *resultArray = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", bookId];
    @try{
        resultArray = (NSArray*) [CoreDataHelper searchObjectsInContext:@"Book" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e){
        DEBUG_LOG(@"search by phone number error: %@", [e description]);
    }
    if (resultArray != nil && [resultArray count] > 0) {
        Book *book = [resultArray objectAtIndex:0];
        return book;
    }
    
    return nil;
}

+ (NSArray*)searchByTitle:(NSString*)bookName{
	NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
    if (managedObjectContext == nil){
        return nil;
    }
    NSArray *resultArray;
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"title contains[cd] %@", bookName];
    @try{
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Book" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e){
        DEBUG_LOG(@"search by mobile number error: %@", [e description]);
    }
    
    return resultArray;
}

+ (BOOL)deleteBook:(int)bookId{
	NSError *err;
	NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
    if (managedObjectContext == nil){
        return NO;
    }
	
	Book *book = [Book bookWithId:bookId];
	if (!book) {
		return NO;
	}else{
		[managedObjectContext deleteObject:book];
		if ([managedObjectContext save:&err])
		return YES;
	}
	return NO;
}

@end
